//
//  FuncA.swift
//  swiftStudy
//
//  Created by unisedu on 16/4/1.
//  Copyright © 2016年 CaoFei. All rights reserved.
//

import UIKit

class FuncA: NSObject {
    //函数的定义和基本用法
    //返回多个值的函数
    //-> String 返回类型
    func sayHello(personName:String) ->String
    {
        
        let greeting = "hello" + personName + "!"
        return greeting
    }
    
   
    func add(a:Int , b:Int) -> Int
    {
        return a+b
    }
  
    func process() -> Float
    {
        return 3*20
    }
    
    func method(){
        print("asdfasdf")
    }
   
    
    //返回多个值
    func count(string: String) -> (vowels:Int,consonants:Int,others:Int)
    {
        var vowels = 0
        var consonants = 0
        var others = 0
        
        for c in string.characters
        {
            switch String(c).lowercaseString
            {
            case "a", "e", "i" , "o" , "u":
                ++vowels
            case "b", "c", "d" , "f" , "g","h", "j", "k" , "l" , "m","n", "p", "q" , "r" , "s","t", "v", "w" , "x" , "y","z":
                ++consonants
            default:
                ++others
            }
        }
        
        return (vowels,consonants,others)
    }
    
    /**
     第二节课
     */
    /*
    1. 扩展参数名
    2. 内部参数名和扩展参数名合二为一
    3. 默认参数值
    4. 可变参数
    5. 常量和变量参数
    6. 输入输出参数
    7. 函数类型
    8. 嵌套函数
    */
   
    func process (p1:String,p2:Int) -> String
    {
        return "name:" + p1 + "age:" + String(p2)
    }
    
    
    //外部参数名 name ，age
    func process1 (name p1:String,age p2:Int) -> String
    {
        return "name:" + p1 + "age:" + String(p2)
    }
   
    
    
    // 内部参数名和扩展参数名合二为一
    func process2 (name name:String,age:Int) -> String
    {
        return "name:" + name + "age:" + String(age)
    }
   
    
    //默认参数值
    func process3 (name p1:String = "Mike",age p2:Int = 30) -> String
    {
        return "name:" + p1 + "age:" + String(p2)
    }
    
    //如果有默认值，那么内部参数名自动变成外部参数名
    func process4 (name :String = "John" ,age:Int = 30) -> String
    {
        return "name:" + name + "age:" + String(age)
    }
   
    //可变参数 实际上式一个数组
    
    func process5 (header:String ,strArray:String...) -> String
    {
        var result = header
        //在创建函数是，参数不显式的话默认式个常量,所以以下语句会报错
        //header = "abc"
        
        for s in strArray
        {
            result += " " + s
        }
        return result
    }
    
    
    func process6 (var header:String ,strArray:String...) -> String
    {
        //参数以值传递 以下不影响实际变量
        // header = "aa"
        for s in strArray
        {
            header += " " + s
        }
        return header
    }
    
    
    func process7 (inout name:String,age:Int) -> String
    {
        name = "Mike"
        return "Name:" + name + "Age:" + String(age)
    }
    
    func process7_1 (inout name:String,age:Int) -> String
    {
        name = "Mike"
        return  name + "Age:" + String(age)
    }
  
    
    func process8 (fun:(inout String,Int) -> String ,str:String){
        var name = ""
        print(fun(&name,40))
    }
    
  
    
    
    func method1(n:Int ) ->Int
    {
        return n*n
    }
    
    
    func method2(n:Int ) ->Int
    {
        return 2*n
    }
    
    func process9(flag:Bool) -> (Int) -> Int
    {
        return flag ? method1 : method2
    }
    
    
 
    
    
    
    //函数嵌套函数
    func process10(flag:Bool ,m:Int,n:Int) -> Int
    {
        func method1() -> Int {
            return m + n
        }
        func method2() -> Int {
            return m - n
        }
        return flag ? method1() : method2()
    }
    
   
    
    
    
    
    
    
    func show(){
        let total = count ("some arbitrary!")
        print("\(total.vowels) vowels and \(total.consonants) consonants")
        print(sayHello("李宁"))
        print(add(20,b: 30))
        print(process())
        method()
        /**
        *  第二节课
        */
        
        process ("bill",p2: 20)
        print(process1(name:"Mike",age:17))
        print(process2(name:"Bill",age:30))
        print(process3())
        print(process3(name:"John"))
        
        print(process4("Mike"))
        
        print (process5("bill",strArray: "a","a","a","a"))
        
        print (process6("bill",strArray: "a","a","a","a"))
        
        //不能直接给name扶植
        //println(process7("bill",40))
        //必须初始化
        var name:String="bill"
        print(process7(&name, age: 40))
        print(name)
        
        //函数类型
        var fun1:(inout String ,Int) ->String
        var fun1_1:(inout String ,Int) ->String
        var fun2:(String, String...) ->String
        
        
        fun1 = process7
        fun1_1 = process7_1
        //fun1 = process6
        
        print(fun1(&name,50))
        process8 (fun1, str: "hello")
        process8 (fun1_1, str: "hello")
        print(process9(true)(20))
        print(process9(false)(20))
        print(process10(true, m: 10,n: 20))
        print(process10(false, m: 10,n: 20))
        
        
    }
    
    
    
    
}
